Repository navigation
fix(templates): allow SSR web workers to fetch separate Cloudflare server worker - #1291
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes. ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughNuxt website generation adds the ChangesCloudflare compatibility flags
Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to
|
Summary
When the web app and the server are deployed as separate Cloudflare Workers on the same account, a
fetch()from the web worker to the server worker'sworkers.devURL fails unless the caller has theglobal_fetch_strictly_publiccompatibility flag or uses a service binding. The generatedalchemy.run.tsonly set that flag for Next.js.This PR adds
global_fetch_strictly_publicto the generated Cloudflare web workers that call the separate server worker during server rendering:authMiddlewarecallsauthClient.getSessionwithVITE_SERVER_URL, and SSR queries go through the oRPC/tRPC linkauth-clientplugin anduseAuthSession(useFetch) run during SSR againstserverUrldeferStream: trueand runs during SSR (forwarding request headers)authClient.getSessionclientLoader, and queries run in the browserssr = false, and queries run in the browserFixes #1028
Why the flag and not a service binding
global_fetch_strictly_publicas making globalfetch()"strictly route requests as if they were made on the public Internet". Without it, requests to Cloudflare-hosted hostnames are sent to the origin and skip Workers mapped to the URL: https://developers.cloudflare.com/workers/configuration/compatibility-flags/#global-fetch-strictly-publicenv: { SERVER: serverWorker }per https://alchemy.run/cloudflare/compute/workers) would avoid the public hop. However, it would need framework-specific app code in every frontend (the oRPC/tRPC links, the better-auth client'scustomFetchImpl, and dev-mode fallbacks). The flag is a one-line config change and matches what the generator already does for Next.js.compatibility.flagsthrough forCloudflare.Website.ViteandCloudflare.Website.Nuxt(fromWorkerProps) and merges them with its defaults (https://alchemy.run/cloudflare/compute/workers, "Compatibility flags and the compatibility date go oncompatibility").The flag is set on these frameworks for every topology, as it already is for Next.js. When the frontend has no separate server, these templates don't make server-side requests to their own URL.
Not verified on a real Cloudflare deployment. I couldn't deploy, so the runtime behavior relies on Cloudflare's documentation.
Verification
webDeploy/serverDeployset tocloudflare, fortanstack-start,nuxtandsolid.bun install,bun run check-types(includingpackages/infra) andbun run buildall pass. The generatedalchemy.run.tshasflags: ["nodejs_compat", "global_fetch_strictly_public"]on the web worker. Areact-routercontrol still has["nodejs_compat"].BTS_BUILD_SAMPLES=1 BTS_BUILD_SAMPLE_FILTER=cloudflare bun test test/generated-builds.test.ts: 5 passBTS_BUILD_SAMPLES=1 BTS_BUILD_SAMPLE_FILTER=workers bun test test/generated-builds.test.ts: 3 passbun run test:ci: 112 pass, 0 failbun run check: cleanSummary by CodeRabbit